package jeecg.business.controller.keyinfo;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyinfo.KeyinfoTextEntity;
import jeecg.business.service.enter.EnterServiceI;
import jeecg.business.service.keyinfo.KeyinfoServiceI;
import jeecg.business.service.keyinfo.KeyinfoTextServiceI;
import jeecg.system.service.SystemService;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: Controller
 * @Description: 舆情信息
 * @author LinYingPiaoJian
 * @date 2015-06-06 16:50:41
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/keyinfoController")
public class KeyinfoController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(KeyinfoController.class);

	@Autowired
	private KeyinfoServiceI keyinfoService;
	@Autowired
	KeyinfoTextServiceI textService;
	@Autowired
	private SystemService systemService;
	@Autowired
	EnterServiceI enterService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 舆情信息列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "keyinfo")
	public ModelAndView keyinfo(HttpServletRequest request) {
		request.setAttribute("qyid", request.getParameter("qyid"));
		return new ModelAndView("jeecg/business/keyinfo/keyinfoList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(KeyinfoEntity keyinfo,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(KeyinfoEntity.class, dataGrid);
		//查询条件组装器
		if(keyinfo != null){
			if(StringUtil.isNotEmpty(keyinfo.getTitle())){
				keyinfo.setTitle("*"+keyinfo.getTitle()+"*");
//				keyinfo.setLtitle(null);
//				keyinfo.setMtitle(null);
//				keyinfo.setRltitle(null);
//				keyinfo.setRmtitle(null);
//				keyinfo.setRstitle(null);
			}
			
			if(StringUtil.isNotEmpty(keyinfo.getKeyword()))
				keyinfo.setKeyword("*"+keyinfo.getKeyword()+"*");
		}
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, keyinfo);
		this.keyinfoService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除舆情信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(KeyinfoEntity keyinfo, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		keyinfo = systemService.getEntity(KeyinfoEntity.class, keyinfo.getId());
		message = "删除成功";
		keyinfoService.delete(keyinfo);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}
	
	@RequestMapping(params = "deledkeyinfo")
	public ModelAndView deledkeyinfo(HttpServletRequest request) {
		request.setAttribute("qyid", request.getParameter("qyid"));
		request.setAttribute("sfsc", "1");
		return new ModelAndView("jeecg/business/keyinfo/del_keyinfo_list");
	}
	
	/**
	 * 更新状态
	 * 
	 * @return
	 */
	@RequestMapping(params = "setStatus")
	@ResponseBody
	public AjaxJson setStatus(KeyinfoEntity keyinfo, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String ids = request.getParameter("ids");
		String status = request.getParameter("status");		//0有效	1删除
		message = "删除成功";
		keyinfoService.setStatus(ids, status);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加舆情信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(KeyinfoEntity keyinfo, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(keyinfo.getId())) {
			message = "更新成功";
			KeyinfoEntity t = keyinfoService.get(KeyinfoEntity.class, keyinfo.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(keyinfo, t);
				keyinfoService.saveKeyinfo4Edit(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			keyinfoService.saveKeyinfo4Edit(keyinfo);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}

	/**
	 * 舆情信息列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(KeyinfoEntity keyinfo, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(keyinfo.getId())) {
			keyinfo = keyinfoService.getEntity(KeyinfoEntity.class, keyinfo.getId());
			EnterEntity enter = enterService.get(EnterEntity.class, keyinfo.getQyid());
			req.setAttribute("qymc", enter.getName());
			KeyinfoTextEntity text = systemService.get(KeyinfoTextEntity.class, keyinfo.getId());
			req.setAttribute("text", text);
			String qt = req.getParameter("qt");
			if("1".equals(qt)){
				req.setAttribute("keyinfoPage", keyinfo);
				return new ModelAndView("jeecg/business/keyinfo/keyinfo_return");
			}
		}else{
			if(keyinfo == null) keyinfo = new KeyinfoEntity();
			keyinfo.setSfsg("1");
			keyinfo.setEventdate(new Date());
		}
		req.setAttribute("keyinfoPage", keyinfo);
		return new ModelAndView("jeecg/business/keyinfo/keyinfo");
	}
	
	@RequestMapping(params = "inreport")
	public ModelAndView inreport(KeyinfoEntity keyinfo, HttpServletRequest req) {
		keyinfo = keyinfoService.get(KeyinfoEntity.class, keyinfo.getId());
		req.setAttribute("keyinfo", keyinfo);
		return new ModelAndView("jeecg/business/keyinfo/inreport");
	}
}
